package com.balance.config.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import com.balance.utils.redis.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Title: CharacterEncodingInterceptor
 * @Author 小东
 * @Package com.balance.config.interceptor
 * @Date 2025/3/28 15:29
 * @description: 处理乱码问题
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    // 注入处理字符串redis对象
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO 拦截请求，判断用户类型
        // 获取请求头token
        String token = request.getHeader("token");
        // 判断redis中是否存在token
        if(StpUtil.getLoginIdByToken(token) == null){
            response.setStatus(401);
            return false;
        }
        if(!(StpUtil.isLogin())){
            response.setStatus(401);
            return false;
        }
        System.out.println(token.equals(StpUtil.getTokenValue()));
        StpUtil.checkLogin();
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        return true;
    }

}
